package com.lbd.loan.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.loan.bean.Loan;
import com.lbd.loan.controller.vo.LoanVO;
import com.lbd.loan.dao.LoanRepository;
import com.lbd.loan.service.LoanService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wang on 2016/9/22.
 */
@Service
@Transactional
public class LoanServiceImpl extends BaseServiceImpl<Loan, LoanRepository> implements LoanService {

    private final Logger log = Logger.getLogger(this.getClass());

    //考虑采用参数字典配置
    private static final int LOAN_APPLY_INTERVAL = 2;

    @Autowired
    LoanRepository loanRepository;

    @Override
    public int loanApply(Loan loan) {
        int checkBeforeResult = checkBeforeLoanApply(loan);
        //检查结果为0时，即通过，保存贷款申请
        if (checkBeforeResult == 0) {
            super.save(loan);
        }
        return checkBeforeResult;
    }


    public int checkBeforeLoanApply(Loan loan) {
        int checkResult = 0;
        //根据申请人手机号，查询所有已存在贷款申请
        List<Loan> loanList = findLoanByMobile(loan.getAppMobile());
        for (int i = 0; i < loanList.size(); i++) {
            Loan loanApplyed = loanList.get(i);
            //首先判断是否存在 1-已受理
            if (1 == loanApplyed.getAppStatus()) {
                checkResult = 1;
                break;
            } else if (0 == loanApplyed.getAppStatus()) {
                //然后判断 0-已申请 的贷款申请日期与当前日期差值是否小于两天
                Date applyedCreatedDate = DateUtils.addDays(loanApplyed.getCreatedDate(), LOAN_APPLY_INTERVAL);
                boolean isAfter = applyedCreatedDate.after(new Date());
                if (isAfter == true) {
                    checkResult = 2;
                    break;
                }
            }
        }

        return checkResult;
    }

    @Override
    public String buildJpql(Loan loan, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  Loan t   ");
        LoanVO loanVO = (LoanVO) loan;
        if (loanVO != null) {
            if (StringUtils.isNotBlank(loanVO.getAppName())) {
                jpqlBuilder.append(" and  appName like :appName");
                parameter.put("appName", "%" + loanVO.getAppName() + "%");
            }
            if (StringUtils.isNotBlank(loanVO.getAppMobile())) {
                jpqlBuilder.append(" and  appMobile like :appMobile");
                parameter.put("appMobile", "%" + loanVO.getAppMobile() + "%");
            }
            if (null != loanVO.getStartDate()) {
                jpqlBuilder.append(" and  createdDate >= :startDate ");
                parameter.put("startDate", loanVO.getStartDate());
            }
            if (null != loanVO.getEndDate()) {
                jpqlBuilder.append(" and  createdDate <= :endDate");
                parameter.put("endDate", loanVO.getEndDate());
            }
            if (StringUtils.isNotBlank(loanVO.getCityName())) {
                jpqlBuilder.append(" and  cityName like :cityName");
                parameter.put("cityName", "%" + loanVO.getCityName() + "%");
            }
            if (null != loanVO.getAppStatus()) {
                jpqlBuilder.append(" and  appStatus = :appStatus");
                parameter.put("appStatus", loanVO.getAppStatus());
            }
            jpqlBuilder.append(" order by createdDate desc ");
        }
        return jpqlBuilder.toString();
    }


    @Override
    public List<Loan> findLoanByMobile(String mobile) {
        return loanRepository.findByAppMobile(mobile);
    }

    @Override
    public List<Loan> findLoanByNameAndMobile(String name, String mobile) {
        return loanRepository.findByAppNameAndAppMobile(name, mobile);
    }

    @Override
    public List<Loan> findAll(Loan loan) {
        return loanRepository.findAll(Example.of(loan));
    }

    @Override
    public Loan findById(String id) {
        return loanRepository.findOne(id);
    }

}
